// #include "ComponentBodyBase.h"
// #include "Managers/GameManager.h"
// #include "Components/ComponentLocomotion.h"
// #include "Components/ComponentCreature.h"
// #include "Components/ComponentHealth.h"
// #include "Components/ComponentDamage.h"

// #include "Managers/BlocksManager.h"
// #include "Managers/EntityManager.h"
// #include "Subsystems/SubsystemTerrain.h"
// #include "Subsystems/SubsystemBodies.h"
// #include "Subsystems/SubsystemTimeOfDay.h"
// #include "Log.h"
// #include "Terrain/ScSimplexNoise.h"
// #include "GameRandom.hpp"

// #include "Subsystems/BlockBehaviors/SubsystemBlockBehaviors.h"
// #include "Subsystems/BlockBehaviors/SubsystemWaterBlockBehavior.h"

// #include "Network/PackageManager.h"
// #include "Network/Packages/SubsystemBodyPackage.h"
// #include "GameSingleton.h"
// #include "Server/EventManager.h"
// #include <cfloat>

// namespace PocketSurvival
// {
//     void ComponentBodyBase::Update(float dt)
//     {
//         SubsystemTerrain *subsystemTerrain = GameSingleton::singletonPtr->getSingleton<SubsystemTerrain>();
//         EventManager *eventManager = GameSingleton::singletonPtr->getSingleton<EventManager>();
//         SubsystemTimeOfDay *timeOfDay = GameSingleton::singletonPtr->getSingleton<SubsystemTimeOfDay>();
//         SubsystemWaterBlockBehavior *waterBlockBehavior = GameSingleton::singletonPtr->getSingleton<SubsystemWaterBlockBehavior>();
//         GameRandom *gameRandom = GameSingleton::singletonPtr->getSingleton<GameRandom>();

//         auto view = GameSingleton::singletonPtr->getSingleton<GameManager>()->getEntityView<ComponentBody *>();
//         for (auto entity : view)
//         {
//             ComponentBodyBase *body = dynamic_cast<ComponentBodyBase*>(view.get<ComponentBody *>(entity));

//             Vector3 &bodyVelocity = body->velocity;

//             body->realPositionMutex.lock();
//             body->realPosition = body->position;
//             body->realPositionMutex.unlock();

//             // body->lastPosition = body->position;

//             if (bodyVelocity.lengthSquared() < 0.001f || bodyVelocity.lengthSquared() >= 9999.0f)
//             {
//                 bodyVelocity = FastVector3::Zero;
//             }

//             bodyVelocity += body->totalImpulse;
//             body->totalImpulse = FastVector3::Zero;

//             // 实时位置检测
//             eventManager->creatureEvent->realTimePosition(body);

//             const Vector3 &pos = body->position;
//             TerrainChunk *chunkAtCell = subsystemTerrain->getTerrainChunk(Terrain::ToCell(pos.posX) >> 4, Terrain::ToCell(pos.posZ) >> 4);
//             if (chunkAtCell == nullptr || chunkAtCell->State <= TerrainChunkState::InvalidContents4)
//             {
//                 bodyVelocity = FastVector3::Zero;
//                 continue;
//             }

//             // 挤压伤害暂时不考虑实体
//             body->bodiesCollisionBoxes.clear();
//             // body->findBodiesCollisionBoxes(body->position, body->bodiesCollisionBoxes);


//             body->movingBlocksCollisionBoxes.clear();
//             body->findMovingBlocksCollisionBoxes(body->position, body->movingBlocksCollisionBoxes);

//             // 卡住地形中受伤
//             if (body->moveToFreeSpace(0.1f) == false)
//             {
//                 if (body->componentCreature != nullptr)
//                 {
//                     if (body->crushInjureTime >= 1.0f)
//                     {
//                         body->componentCreature->componentHealth->injure(0.15f, entt::null, true, "挤压而死");
//                         body->crushInjureTime = 0.0f;
//                     }
//                     body->crushInjureTime += dt;
//                 }
//                 else
//                 {
//                     Log::Warn("出现位置的实体body错误");
//                 }
//                 continue;
//             }
//             else
//             {
//                 body->crushInjureTime = 0.0f;
//             }

//             if (body->componentPlayer != nullptr)
//             {
//                 continue;
//             }

//             bodyVelocity.posY -= 10.0f * dt;
//             if (body->immersionFactor > 0.0f)
//             {
//                 float num = body->immersionFactor * (1.0f + 0.03f * MathUtils::Sin<float>((float)MathUtils::Remainder(2.0 * timeOfDay->getGameTime(), 6.2831854820251465)));
//                 bodyVelocity.posY += 10.0f * (1.0f / body->density * num) * dt;
//             }


//             float num2 = MathUtils::Saturate(body->airDrag.posX * dt);
//             float num3 = MathUtils::Saturate(body->airDrag.posY * dt);
//             bodyVelocity.posX *= 1.0f - num2;
//             bodyVelocity.posY *= 1.0f - num3;
//             bodyVelocity.posZ *= 1.0f - num2;
//             if(bodyVelocity.lengthSquared() < 0.0002f)
//             {
//                 bodyVelocity = FastVector3::Zero;
//             }

//             if (body->immersionFactor > 0.0f && body->immersionFluidBlock != nullptr)
//             {
//                 std::shared_ptr<Vector2> vector = waterBlockBehavior->calculateFlowSpeed(Terrain::ToCell(pos.posX), Terrain::ToCell(pos.posY), Terrain::ToCell(pos.posZ));
//                 Vector3 vector2 = (vector != nullptr ? Vector3(vector->posX, 0.0f, vector->posY) : FastVector3::Zero);
//                 float num4 = 1.0f;
//                 if (body->immersionFluidBlock->FrictionFactor != 1.0f)
//                 {
//                     num4 = ((ScSimplexNoise::Noise((float)MathUtils::Remainder(6.0 * dt + (double)(gameRandom->getInt() % 1000), 1000.0)) > 0.5f) ? body->immersionFluidBlock->FrictionFactor : 1.0f);
//                 }
//                 float f = MathUtils::Saturate(body->waterDrag.posX * num4 * body->immersionFactor * dt);
//                 float f2 = MathUtils::Saturate(body->waterDrag.posY * num4 * dt);

//                 bodyVelocity.posX = MathUtils::Lerp(bodyVelocity.posX, vector2.posX, f);
//                 bodyVelocity.posY = MathUtils::Lerp(bodyVelocity.posY, vector2.posY, f2);
//                 bodyVelocity.posZ = MathUtils::Lerp(bodyVelocity.posZ, vector2.posZ, f);
//                 if (vector != nullptr && body->standingOnValue == 0)
//                 {
//                     Quaternion quration;
//                     if (body->waterTurnSpeed > 0.0f)
//                     {
//                         float s = MathUtils::Saturate(MathUtils::Lerp(1.0f, 0.0f, bodyVelocity.length()));
//                         Vector2 vector3 = Vector2::Normalize(*vector) * s;
//                         Quaternion::CreateFromAxisAngle(FastVector3::UnitY, body->waterTurnSpeed * (-1.0f * vector3.posX + 0.71f * vector3.posY) * dt, quration);
//                         quration *= body->rotation;
//                         body->setRotation(quration);
//                     }
//                     if (body->waterSwayAngle > 0.0f)
//                     {
//                         Quaternion::CreateFromAxisAngle(FastVector3::UnitX, body->waterSwayAngle * (float)MathUtils::Sin((double)(200.0f / body->mass) * timeOfDay->getGameTime()), quration);
//                         quration *= body->rotation;
//                         body->setRotation(quration);
//                     }
//                 }
//             }

//             body->standingOnValue = 0;
//             body->standingOnBody = nullptr;
//             body->standingOnVelocity = FastVector3::Zero;
//             Vector3 velocity = bodyVelocity;
//             float num5 = bodyVelocity.length();
//             if (num5 > 0.0f)
//             {
//                 Vector3 stanceBoxSize = body->getStanceBoxSize();
//             	float x = 0.45f * MathUtils::Min<float>(stanceBoxSize.posX, stanceBoxSize.posY, stanceBoxSize.posZ) / num5;
//             	float num6 = dt;
//             	while (num6 > 0.0f)
//             	{
//             		float num7 = MathUtils::Min<float>(num6, x);
//                     Vector3 theVelocity = bodyVelocity * num7 + body->directMove;
                    
//             		body->moveWithCollision(num7, theVelocity);
//             		body->directMove = FastVector3::Zero;
//             		num6 -= num7;
//             	}
//             }

//             body->collisionVelocityChange = bodyVelocity - velocity;
//             if (body->standingOnValue != 0)
//             {
//             	Vector3::Lerp(velocity, body->standingOnVelocity, 6.0f * dt, bodyVelocity);
//             }
//             body->updateImmersionData();
//         }
//     }

//     void ComponentBodyBase::load(const nlohmann::json &jsonData)
//     {
//         ComponentBody::load(jsonData);
//     }


//     static const std::string componentName = "Body";
//     const std::string &ComponentBodyBase::getName() const
//     {
//         return componentName;
//     }
//     const std::string &ComponentBodyBase::GetName()
//     {
//         return componentName;
//     }
// } // namespace PocketSurvival
